package com.msb.strategy;

/**
 * @Auther： WangLei
 * @Date： 2021/9/9-09-09-23:05
 * @Description: com.msb.strategy
 * @version: 1.0
 */
public class Sorter<T> {

    /*public void sort(MyComparable[] a) {
        *//*for(int i = 0; i < a.length; i++){
            int minPos = i;
            for(int j = i + 1;j < a.length;j++){
                minPos = (a[minPos] > a[j])? j : minPos;
            }
            if (i != minPos) swap(a,i,minPos);
        }*//*
        for(int i = 0; i < a.length; i++){
            int minPos = i;
            for(int j = i + 1;j < a.length;j++){
                minPos = (a[minPos].compareTo(a[j]))? j : minPos;
            }
            if (i != minPos) swap(a,i,minPos);
        }
    }*/

    public void sort(T[] a,MyComparator c) {
        for(int i = 0; i < a.length; i++){
            int minPos = i;
            for(int j = i + 1;j < a.length;j++){
                minPos = (c.compare(a[minPos],a[j]))? j : minPos;
            }
            if (i != minPos) swap(a,i,minPos);
        }
    }


    private void swap(T[] a,int i,int j){
        T tmp = a[i];
        a[i] = a[j];
        a[j] = tmp;
    }
}
